#include <bits/stdc++.h>
using namespace std;

void extended_gcd(int a,int b,int& x,int& y)
{
	if(b==0)
	{
		x=1;
		y=0;
		return;
	}
	else
	{
		extended_gcd(b,a%b,x,y);
		int t=y;
	    y=x-(a/b)*y;
	    x=t;
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	
	int a,b,x,y;
	cin >> a >> b;
	x=0,y=0;
	extended_gcd(a,b,x,y);
	while(x<=0)
	{
		x=x+b;
	}
	cout << x << endl;
	return 0;
}
